package com.pan.api.utils;

enum GetUtilInstanceUtilImpl implements GetUtilInstanceUtil, PanApiDocXmlUtil {
    INSTANCE;

    @Override
    public <T> T getUtil(String... configName) {
        String utilClassFullName = findFirst(configName).getTextContent().strip();
        ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
        try {
            Class<T> tarClass = (Class<T>) systemClassLoader.loadClass(utilClassFullName);
            if (!tarClass.isEnum())
                throw new RuntimeException("This util class is not the enum type: ".concat(utilClassFullName));
            T[] enumConstants = tarClass.getEnumConstants();
            if (enumConstants.length < 1)
                throw new RuntimeException("This util class has not instance: ".concat(utilClassFullName));
            return enumConstants[0];
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

